package com.weavernorth.attendance;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * @program: ecology_erdos
 * @description: 补发年假sql类
 * @author: zm
 * @create: 2019-12-24 09:34
 **/


public class AnnualLeaveUtil extends BaseBean {
    public static Log log = LogFactory.getLog(AnnualLeaveUtil.class.getName());

    /**
     * 得到工领及计算补发年假时长
     *
     * @param strUserid 用户id  strTenYear参加工作满10年日期
     *                  strTenYear参加工作满20年日期 strJoinDate首次参加工作日期  strCurrDate 当前日期
     */

    public void getAnnualLeaveTime(String strUserid, String strMonth, String strOneYear, String strTenYear, String strTwentyYear, String strJoinDate, String strCurrDate) {
        log.info("进入计算补发年假时长,人员:" + strUserid + ",入职日期:" + strMonth + ",参加工作满1年日期:" + strOneYear + ",参加工作满10年日期:" + strTenYear + ",满20年日期:" + strTwentyYear + ",首次参加工作:" + strJoinDate + ",当前日期:" + strCurrDate);
        //数据库操作
        RecordSet rs = new RecordSet();
        //基础类
        BaseBean bb = new BaseBean();

        //获取多长时间补发年假
        int intWorkMonth = Util.getIntValue(bb.getPropValue("annualMess", "annLeaveTime"));
        //满1年不满10年的年假天数
        int intTen = Util.getIntValue(bb.getPropValue("annualMess", "firstDay"));
        //满10年不满20年的年假天数
        int intTwenty = Util.getIntValue(bb.getPropValue("annualMess", "secondDay"));
        //满20年的年假天数
        int intMoreTwenty = Util.getIntValue(bb.getPropValue("annualMess", "thirdDay"));
        //当前年份
        String strCurrYear = "";
        String strNowMonth="";
        if (!"".equals(strCurrDate)) {
            strCurrYear = strCurrDate.split("-")[0];
            strNowMonth=strCurrDate.split("-")[1];
        }
        //当前年的元旦
        String strBegin = strCurrYear + "-01-01";

        //判断入职至转正日期之间是否跨年,转正日期即当前日期距离1月1号超过配置文件里的天数 ,超过则不跨年 ,否则跨年
        int intDiffDays = differentDayMillisecond(strBegin, strCurrDate);
        //获取当前年假基数
        int intBaseDay = 0;
        //如果当前日期在1年和10年之间,则基数为配置文件中满1年不满10年的年假天数
        if (strOneYear.compareTo(strCurrDate) <= 0 && strTenYear.compareTo(strCurrDate) > 0) {
            intBaseDay = intTen;
        } else if (strTenYear.compareTo(strCurrDate) <= 0 && strTwentyYear.compareTo(strCurrDate) > 0) {
            intBaseDay = intTwenty;
        } else if (strTwentyYear.compareTo(strCurrDate) <= 0) {
            intBaseDay = intMoreTwenty;
        }
        log.info("当前年假基数:" + intBaseDay);
        Integer monthNow=Integer.parseInt(strNowMonth);
        log.info("获取当前月份："+monthNow);
        //年假按月补发,因此要扣掉当前月到12月的年假,只发当月之前的,后边的年假逐月释放
        Double synj=(12-monthNow)*(Double.valueOf(intBaseDay)/12.00);
        log.info("获取当前应扣掉的假期："+synj);
        String strFlag = existUser(strUserid, strCurrYear);
        log.info("当前人员是否存在假期表里:" + strFlag);
        //跨年,则把去年的年假补发到额外年假字段里
        if (intDiffDays < intWorkMonth) {
            //入职年的年假基数
            int intBaseDayRz = 0;
            //如果入职日期在1年和10年之间,则基数为配置文件中满1年不满10年的年假天数
            if (strOneYear.compareTo(strMonth) <= 0 && strTenYear.compareTo(strMonth) > 0) {
                intBaseDayRz = intTen;
            } else if (strTenYear.compareTo(strMonth) <= 0 && strTwentyYear.compareTo(strMonth) > 0) {
                intBaseDayRz = intTwenty;
            } else if (strTwentyYear.compareTo(strMonth) <= 0) {
                intBaseDayRz = intMoreTwenty;
            }
            //计算入职日期到1月1日的年假天数
            //跨年：额外年假：次年1月1到入职日期间的天数
            int day1= differentDayMillisecond(strMonth, strBegin);
            Double douyearLeave1 = Double.valueOf(day1) / 365 * Double.valueOf(intBaseDayRz);
            //跨年：1月1号到转正日期
            Double douYearLeave2=Double.valueOf(180-day1)/ 365 * Double.valueOf(intBaseDayRz);
            Double extraAmount=douyearLeave1+douYearLeave2;
            if(extraAmount>intBaseDay){
                extraAmount=extraAmount-intBaseDay;
            }
            Double doubUsed=intBaseDay-extraAmount+synj;

            String strResult = String.format("%.2f", extraAmount);
            log.info("跨年补发的额外年假:" + strResult);

            if ("".equals(strFlag)) {
                String insertSql = "insert into kq_balanceofleave(baseAmount,usedAmount,resourceid,belongyear,leaverulesid) " +
                        "values(?,?,?,?,?)";
                rs.executeQuery(insertSql, intBaseDay, strResult, strUserid, strCurrYear, "2");

            } else {
                //更新到额外假期余额字段
                String upSql = "update kq_balanceofleave set baseAmount=" + intBaseDay + ", usedAmount =" + strResult + " where resourceid=? and leaverulesid='2' and belongyear =?";
                log.info("补发年假更新表字段:" + upSql + ",strUserid:" + strUserid + "," + strCurrYear);
                rs.executeQuery(upSql, strUserid, strCurrYear);
            }

        }
        //不跨年
        else {
            //获取次年的一月一日
            String dates=(Integer.parseInt(strCurrYear)+1)+"-01-01";
            Double douYearLeave = Double.valueOf(differentDayMillisecond(strMonth, dates)) / 365 * Double.valueOf(intBaseDay);
            Double doubUsed=intBaseDay-douYearLeave+synj;
            String strResult = String.format("%.2f", doubUsed);
            log.info("不跨年更新已休年假:" + strResult);
            if ("".equals(strFlag)) {
                String insertSql = "insert into kq_balanceofleave(baseAmount,usedAmount,resourceid,belongyear,leaverulesid) " +
                        "values(?,?,?,?,?)";
                rs.executeQuery(insertSql, intBaseDay, strResult, strUserid, strCurrYear, "2");

            } else {
                //更新到额外假期余额字段
                String upSql = "update kq_balanceofleave set baseAmount=" + intBaseDay + ", usedAmount =" + strResult + " where resourceid=? and leaverulesid='2' and belongyear =?";
                log.info("补发年假更新已休年假表字段:" + upSql + ",strUserid:" + strUserid + "," + strCurrYear);
                rs.executeQuery(upSql, strUserid, strCurrYear);
            }
        }


    }

    /**
     * 计算两个时间字段相差的天数
     */

    public static int differentDayMillisecond(String strDateBegin, String strDateEnd) {
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");
        int day = 0;
        try {
            Date dateBegin = format1.parse(strDateBegin);
            Date dateEnd = format2.parse(strDateEnd);
            day = (int) ((dateEnd.getTime() - dateBegin.getTime()) / (3600 * 1000 * 24));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return day;
    }

    //判断数据库是否有此人员
    public String existUser(String strResourceid, String strBelongYear) {
        RecordSet rs = new RecordSet();
        String strResult = "";
        String strSql = "select 1 from kq_balanceofleave where  resourceid=? and leaverulesid='2' and belongyear =?";
        rs.executeQuery(strSql, strResourceid, strBelongYear);
        if (rs.next()) {
            strResult = "fail";
        }

        return strResult;

    }

}
